using System.Collections.Generic;
using System.Linq;
using EasyCMMS.Services.Domain;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Search;

namespace EasyCMMS.Services.Repositories
{
    public class Repository<T> : IRepository<T>
    {
        private IQueryable<T> linq;
        private ISession session;


        public Repository(ISession session)
        {
            this.session = session;
        }

        #region IRepository<T> Members

        public IQueryable<T> Linq
        {
            get
            {
                if (linq == null)
                {
                    return session.Linq<T>();
                }
                return linq;
            }
            set
            {
                linq = value;
            }
        }

        public IList<T> ExecuteFullTextQuery(string query)
        {
            IFullTextSession fullTextSession = session as IFullTextSession;

            if(fullTextSession == null)
            {
                fullTextSession = Search.CreateFullTextSession(session);
            }

            return fullTextSession.CreateFullTextQuery<T>(query).List<T>();
        }

        #endregion
    }
}